package mars;

import mars.RotateCommand.RotateAngle;

public class Rover {

	Position currentPosition;
	String commandString;
	
	Command command;
	RotateCommand rotateCommand;
	MoveCommand moveCommand;
	
	public Rover(Position currentPosition) {
		this.currentPosition = currentPosition;
		rotateCommand = new RotateCommand();
		moveCommand = new MoveCommand();
	}

	public Position getCurrentPosition() {
		return currentPosition;
	}
	
	public void processCommand(String commandString) throws OutOfBoundsException, ParseException {
		for(int i = 0; i < commandString.length(); i++) {
			char currCommand = commandString.charAt(i);
			switch (currCommand) {
			case 'L':
				rotateCommand.setRotateAngle(RotateAngle.L);
				setCommand(rotateCommand);
				break;
			case 'R':
				rotateCommand.setRotateAngle(RotateAngle.R);
				setCommand(rotateCommand);
				break;
			case 'M':
				setCommand(moveCommand);
				break;
			default:
				throw new ParseException("Command string can only consist of L, R or M values");
			}
			currentPosition = command.process(currentPosition);
		}
	}
	
	private void setCommand(Command command) {
		this.command = command;
	}
}
